<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>
<link rel="stylesheet"
	href="${pageContext.request.contextPath }/frame/layui/css/layui.css" />
<link rel="icon"
	href="${pageContext.request.contextPath }/frame/static/image/code.png">
<link rel="stylesheet"
	href="${pageContext.request.contextPath }/frame/static/css/style.css">
</head>
<body>
<!-- 修改部门 -->
<div style="width: 300px; height: 200px; padding: 15px">
	<form id="add_dept_form" class="layui-form"
		method="post" lay-filter="add_form_filter">
		
		<input type="hidden" id="dept_did_input" name="did" value="${dept.did}"/>
		<div class="layui-form-item">
			<label class="layui-form-label">部门名称：</label>
			<div class="layui-input-block">
				<input id="dept_name" type="text" name="dname" value="${dept.dname}" 
					placeholder="请输入部门名称" class="layui-input "lay-verify="required" >
			</div>
		</div>
		<div class="layui-form-item layui-form-text">
			<label class="layui-form-label">部门备注：</label>
			<div class="layui-input-block">
				<textarea id="dept_remark" name="dremark" 
					placeholder="请输入内容" class="layui-textarea" >${dept.dremark}</textarea>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button id= "update_button" class="layui-btn" type="submit">提交</button>
			</div>
		</div>
	</form>
</div>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/frame/layui/layui.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/index.js"></script>
<script type="text/javascript">
	 layui.use(['element', 'table', 'jquery', 'layer', 'form', 'upload'], function () {
	        var $ = layui.jquery,
	            layer = layui.layer,
	            form = layui.form,
	            upload = layui.upload;
	
	        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
	      	//表单提交事件
            form.on('submit(add_form_filter)', function (data) {
            	//表单输入的部门
                var input_name = $("#dept_name").val();
					//alert(input_name);
                var name = "${dept.dname}";
                //如果相等没有发生修改直接提交
                if (input_name == name) {
                    submit_form();
                } else {
                    //不相等, 正常检查部门是否存在
                    dept_check(input_name);
                }
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });

            //dept检查
            function dept_check(value) {
                $.post("${pageContext.request.contextPath}/deptAction_checkDept.action",
                    {dname: value},
                    function (result) {
                        if (result == 'exist') {
                            // layer.tips 吸附弹出
                            layer.tips('该部门已经存在', "#dept_name");
                            $("#dept_name").focus();
                        } else {
                            submit_form();
                        }
                    });
            }

            function submit_form() {
                $.post("${pageContext.request.contextPath}/deptAction_saveOrupdate.action",
                    $("#add_dept_form").serialize(),
                    function (data) {
                        parent.location.href = '${pageContext.request.contextPath}/page_dept_dept.action';
                      //重新加载数据
        	        	tableIns.reload();
                        parent.layer.close(index);
                    });
            }
	 });

</script>
</body>
</html>